#include <iostream>
#include <algorithm>
#include <unordered_map>
using namespace std;

const int N = 1010;
int a[N * 2], b[N * 2], n;
int disc[N * 2 * 2], pos;
int f[4 * N];
int m;

int find(int x)
{
	int l = 1, r = pos;
	while(l < r)
	{
		int mid = (l + r) / 2;
		if(disc[mid] >= x) r = mid;
		else l = mid + 1;
	}
	return l;
}


int main()
{
	cin >> m >>	n;
	for(int i = 1; i <= 2 * n; i++)
	{
		cin >> a[i] >> b[i];
		disc[++pos] = a[i];
		disc[++pos] = b[i];
		i++;
		a[i] = a[i - 1] + 1;
		b[i] = b[i - 1] + 1;
		disc[++pos] = a[i];
		disc[++pos] = b[i];
	}
	
	sort(disc + 1, disc + 1 + pos);
	pos = unique(disc + 1, disc + 1 + pos) - (disc + 1);
	
	for(int i = 1; i <= 2 * n; i+=2)
	{
		int l = find(a[i]);
		int r = find(b[i]);
		for(int j = l; j <= r; j++)
		{
			f[j] = i;
		}
	} 
	unordered_map<int, int> mp;
	for(int i = 1; i <= pos; i++)
	{
		if(f[i])
		mp[f[i]]++;
	}
	
	cout << mp.size() << endl;
	
	return 0;
}
